package org.abratuhi.kvngroupware.scenario.mptua;

import java.util.Properties;

import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.AbstractResponseMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.scenario.manager.ScenarioManager;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class ScenarioJokeResponseMessageProceeder extends AbstractResponseMessageProceeder{

	LoginManager loginmanager;
	ScenarioManager scenariomanager;
	WidgetManager widgetmanager;

	public ScenarioJokeResponseMessageProceeder(MessageSender sender, Properties locale, LoginManager loginmanager, ScenarioManager scenariomanager, WidgetManager widgetmanager){
		super(sender, locale);
		this.loginmanager = loginmanager;
		this.scenariomanager = scenariomanager;
		this.widgetmanager = widgetmanager;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element response = ((Element) message.getRoot().getChildren("response").get(0));
		String type = response.getAttributeValue("type");
		String action = response.getAttributeValue("action");
		String jokeId = response.getAttributeValue("jokeId");
		String scenarioId = response.getAttributeValue("scenarioId");
		String status = response.getAttributeValue("status");

		if(type.equals("scenario_joke")){
			if(status.equals("ok")){
				if(action.equals("add")){
					scenariomanager.get(scenarioId).add(jokeId);
					if(widgetmanager.get(scenarioId) != null) widgetmanager.get(scenarioId).repaint();
				}
				else if(action.equals("remove")){
					scenariomanager.get(scenarioId).remove(jokeId);
					if(widgetmanager.get(scenarioId) != null) widgetmanager.get(scenarioId).repaint();
				}
				else if(action.equals("up")){
					scenariomanager.get(scenarioId).up(jokeId);
					if(widgetmanager.get(scenarioId) != null) widgetmanager.get(scenarioId).repaint();
				}
				else if(action.equals("down")){
					scenariomanager.get(scenarioId).down(jokeId);
					if(widgetmanager.get(scenarioId) != null) widgetmanager.get(scenarioId).repaint();
				}
			}
		}
	}

}
